<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : User profile
 * @description : Show user details 
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Profile extends AUTH_Controller {

  public function index($user_name = NULL) {

    // If no username is give. set it to current user profile
    if (!$user_name) {
      $user_name = $this->current_user['user_name'];
    }

    // Cleaning username
    $user_name = $this->sanitize($user_name);

    // Checking for username
    if (!empty($user_name)) {

      // Getting user details
      $get_user_by_username = $this->user_model->get_user_by_username($user_name);

      // Checking user details
      if ($get_user_by_username['status'] == 1 && !empty($get_user_by_username['result'])) {

        // Blocking accessing to non user prfoile, redirect to wall
        if ($get_user_by_username['result']['user_is_user'] == 0) {
          redirect('wall/' . $user_name);
        }

        // Block accessing resigned user's profile
        if ($get_user_by_username['result']['user_resigned'] == 1) {
          redirect('users');
        }

        // Getting likes data counts
        $likes_data = $this->get_user_likes($get_user_by_username['result']['user_id']);

        // Preparing view data
        $view_data = array();
        $view_data['menu_active'] = 'users';
        $view_data['user_data'] = $get_user_by_username['result'];
        $view_data['tweet_like_count'] = $likes_data['tweet_like_count'];
        $view_data['album_like_count'] = $likes_data['album_like_count'];
        $view_data['poll_like_count'] = $likes_data['poll_like_count'];
        $view_data['total_likes_count'] = $likes_data['total_likes_count'];
        $view_data['all_likes_count'] = $likes_data['all_likes_count'];

        // Loading view
        $this->load_view('profile', $view_data);
        return;
      }
    }

    // Redirect to users list if something wrong.
    redirect('users');
    return;
  }

  /**
   * Get likes counts of the user
   * 
   */
  private function get_user_likes($user_id = NULL) {

    // Getting users likes count for tweets
    $tweets_like_count = 0;
    $get_tweets = $this->tweet_model->get_my_tweets($user_id, NULL);
    if (!empty($get_tweets['result']) && is_array($get_tweets['result'])) {
      foreach ($get_tweets['result'] as $tweet) {
        $tweets_likes = get_user_like_status('tweet', $tweet['tweet_id']);
        if (!empty($tweets_likes['total_count'])) {
          $tweets_like_count = $tweets_like_count + $tweets_likes['total_count'];
        }
      }
    }

    // Getting users likes count for replies
    $reply_like_count = 0;
    $get_my_replies = $this->tweet_model->get_my_replies($user_id, NULL);
    if (!empty($get_my_replies['result']) && is_array($get_my_replies['result'])) {
      foreach ($get_my_replies['result'] as $reply) {
        $reply_likes = get_user_like_status('reply', $reply['tweet_reply_id']);
        if (!empty($reply_likes['total_count'])) {
          $reply_like_count = $reply_like_count + $reply_likes['total_count'];
        }
      }
    }

    // Getting users likes count for albums
    $album_like_count = 0;
    $get_albums = $this->album_model->get_albums($user_id, NULL);
    if (!empty($get_albums['result']) && is_array($get_albums['result'])) {
      foreach ($get_albums['result'] as $album) {
        $album_likes = get_user_like_status('album', $album['album_id']);
        if (!empty($album_likes['total_count'])) {
          $album_like_count = $album_like_count + $album_likes['total_count'];
        }
      }
    }

    // Getting users likes count for polls
    $poll_like_count = 0;
    $get_polls = $this->poll_model->get_my_polls(NULL, $user_id);
    if (!empty($get_polls['result']) && is_array($get_polls['result'])) {
      foreach ($get_polls['result'] as $poll) {
        $poll_likes = get_user_like_status('poll', $poll['poll_id']);
        if (!empty($poll_likes['total_count'])) {
          $poll_like_count = $poll_like_count + $poll_likes['total_count'];
        }
      }
    }

    // Getting users likes count for comments
    $comment_like_count = 0;
    $get_my_comments = $this->comment_model->get_my_comments($user_id, NULL);
    if (!empty($get_my_comments['result']) && is_array($get_my_comments['result'])) {
      foreach ($get_my_comments['result'] as $comment) {
        $comment_likes = get_user_like_status('comment', $comment['comment_id']);
        if (!empty($comment_likes['total_count'])) {
          $comment_like_count = $comment_like_count + $comment_likes['total_count'];
        }
      }
    }

    // Getting users likes count for photos
    $photo_like_count = 0;
    $get_my_photos = $this->album_model->get_my_photos($user_id);
    if (!empty($get_my_photos['result']) && is_array($get_my_photos['result'])) {
      foreach ($get_my_photos['result'] as $photo) {
        $photo_likes = get_user_like_status('photo', $photo['photo_id']);
        if (!empty($photo_likes['total_count'])) {
          $photo_like_count = $photo_like_count + $photo_likes['total_count'];
        }
      }
    }

    // Getting total likes count on system
    $get_all_likes_by_type = $this->like_model->get_all_likes_by_type();

    $return_data = array();
    $return_data['tweet_like_count'] = (int) ($tweets_like_count + $reply_like_count + $comment_like_count);
    $return_data['album_like_count'] = (int) ($album_like_count + $photo_like_count);
    $return_data['poll_like_count'] = (int) $poll_like_count;
    $return_data['total_likes_count'] = (int) ($return_data['tweet_like_count'] + $return_data['album_like_count'] + $return_data['poll_like_count']);
    $return_data['all_likes_count'] = (int) $get_all_likes_by_type['count'];

    return $return_data;
  }

}

?>